package demo.springboot.Controller.Administrator;
import demo.springboot.POJO.T_news;
import demo.springboot.service.T_newsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static demo.springboot.Tools.Tools2.get_operating_system;


@Controller
@RequestMapping(value = "/admin")
public class adminIndex {

    @Value("${upload-path}")
    private String upload_path;

    @Autowired
    T_newsService newsService;


    @GetMapping(value = "")
    public String getBookList(ModelMap map, Model model) {
        List<T_news> news = newsService.findAll();
        System.out.println(upload_path);
        System.out.println(upload_path);
        System.out.println(upload_path);
        return "admin/index.html";
    }


    /* 上传文件    */
    @PostMapping("/upload")
    public String update(@RequestParam("pic") MultipartFile multipartFile) {
        String file_path = "";
        String operating_system = get_operating_system();
        if (operating_system.equals("windows")){
            file_path = "C:\\img\\";                                  //  windows 系统测试用
        }else {
            file_path = upload_path;                                  //  linux系统
        }

        try {
            String filename_time = System.currentTimeMillis() + "";
            String namess = multipartFile.getOriginalFilename();
            String[]  listttt = namess.split("\\.");
            String  suffix  = listttt[1];
            System.out.println(multipartFile.getOriginalFilename());
            System.out.println(multipartFile.getContentType());
            System.out.println(multipartFile.getSize());

            File file = new File( file_path + filename_time + "." +  suffix );
            multipartFile.transferTo(file);
            return "admin/index.html";
        } catch (IOException e) {
            e.printStackTrace();
            return "error/index.html";     // 程序出现错误之后就跳转到显示错误的页面
        }
    }




}
